package com.taoyibao.hongcanting.base.delegate;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

import com.taoyibao.hongcanting.R;

import java.util.ArrayList;

/**
 * @author TangAnna
 * @description: Tab页面 （头部带有返回箭头）
 * @date :${DATA} 16:17
 */
public class BackViewPageDelegate extends BackToolbarDelegate {
    private FragmentManager mFragmentManager;
    private ArrayList<String> mTitleList = new ArrayList<String>();
    private ArrayList<Fragment> arrayListFragment = new ArrayList<>();
    private FragmentStatePagerAdapter mFragmentStatePagerAdapter;
    public TabLayout tabLayout;
    private ViewPager mViewPager;

    @Override
    public int getRootLayoutId() {
        return R.layout.widget_view_page;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        initViewPageTab();
    }


    public void addViewPageTab(String title, Fragment fragment) {
        mTitleList.add(title);
        arrayListFragment.add(fragment);
        mFragmentStatePagerAdapter.notifyDataSetChanged();
    }

    public void setFragmentManager(FragmentManager mFragmentManager) {
        this.mFragmentManager = mFragmentManager;
    }

    private void initViewPageTab() {
        tabLayout = get(R.id.tab_layout);
        mViewPager = get(R.id.tab_viewPager);
        tabLayout.setupWithViewPager(mViewPager);

        mFragmentStatePagerAdapter = new FragmentStatePagerAdapter(mFragmentManager) {
            @Override
            public int getCount() {
                return mTitleList == null ? 0 : mTitleList.size();
            }

            @Override
            public Fragment getItem(int index)//直接创建fragment对象并返回
            {
                return arrayListFragment.get(index);
            }

            @Override
            public CharSequence getPageTitle(int position) {

                return mTitleList == null ? super.getPageTitle(position) : mTitleList.get(position);
            }
        };

        mViewPager.setAdapter(mFragmentStatePagerAdapter);
    }

    /**
     * 设置activity的背景色
     */
    public void setRootViewColor(int color) {
        get(R.id.page_rootView).setBackgroundColor(color);
    }

    /**
     * 设置tabLayout 背景色
     *
     * @param color
     */
    public void setTabLayoutBackgroundColor(int color) {
        get(R.id.tab_layout).setBackgroundColor(color);
        get(R.id.view_tabBottom).setBackgroundColor(color);
    }

    /**
     * 设置预加载数量
     *
     * @param count
     */
    public void viewPagerOffscreenPageLimit(int count) {
        if (mViewPager != null) {
            mViewPager.setOffscreenPageLimit(count);
        }
    }

    /**
     * 设置当前显示的页面
     *
     * @param currentPager
     */
    public void setCurrentPager(int currentPager) {
        mViewPager.setCurrentItem(currentPager);
    }

    /**
     * 设置tabModel是可滑动的
     */
    public void setTabScrollModel() {
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    }
}
